package ma.ensao.evalmetrics.view;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import ma.ensao.evalmetrics.controller.AffectationProjetManager;
import ma.ensao.evalmetrics.controller.FileManager;
import ma.ensao.evalmetrics.model.MyFile;
import ma.ensao.evalmetrics.model.User;
import ma.ensao.evalmetrics.util.HibernateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/*
* Affectation d'un projet a un User
*/
public class AffectationProjet extends ActionSupport implements SessionAware {
	private List<User> users;
	private List<User> users_chosen;
	private List<Long> users_chosen_ID;
	private List<User> usersProjet;
	private String myFichierContentType;
	private MyFile myFile;
	private List<MyFile> fileList;
	private List<User> userList;
	private Long id;
	private Map<String, Object> session;
	private AffectationProjetManager ApManager;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public AffectationProjet(){
		ApManager= new AffectationProjetManager();
	}
	public String fileUpload() {	
		System.out.println(">>"+users_chosen_ID.size());
		ApManager.upload(myFile, users_chosen_ID);
		return SUCCESS;
	}
	public String usersList(){
		setUsers(ApManager.usersList());
		users_chosen= new ArrayList<User>(users.size());
		System.out.println("fiiiiirst user"+users.get(0).getNom());
		return SUCCESS;
	}
	public String usersProjetList(){
		setUsers_chosen(ApManager.usersProjetList(getId()));
		return SUCCESS;
	}
	public String liste() {
		session = ActionContext.getContext().getSession();
		try {
			this.fileList = ApManager.list((Long) session.get("id"), (String) session.get("statut"));	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String getSpecificProjet() {
		usersList();
		usersProjetList();
		List<User> usersMoinsUserChosen= new ArrayList<User>(users.size());
		System.out.println("users.size="+users.size()+"usersChosen.size"+users_chosen.size());
		for(int i=0; i<users.size(); i++){
			int k=0;
			for(int j=0; j<users_chosen.size(); j++){
				if(users.get(i).getUserLogin().equals(users_chosen.get(j).getUserLogin())){
					k++;
				}
			}
			if(k==0){
				usersMoinsUserChosen.add(users.get(i));
			}
		}
		users= usersMoinsUserChosen;
		for (int i = 0; i < users.size(); i++) {
			System.out.println("users>"+users.get(i).getUserLogin());
		}
		for (int i = 0; i < usersMoinsUserChosen.size(); i++) {
			System.out.println("usersMoinsUserChosen>"+usersMoinsUserChosen.get(i).getUserLogin());
		}
		System.out.println("UserAction getSpecificUser >>>>>>>>>>>>"+ getId());
		try {
			this.setFileList(ApManager.getSpecificProjet(getId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String modifier() {
		System.out.println("modifier projet"+ users_chosen_ID.size());
		for (int i = 0; i < users_chosen_ID.size(); i++) {
			System.out.println("chosen>>>>>>>>>>>>>>>"+users_chosen_ID.size());
		}
			try {
			ApManager.modifier(getMyFile(), users_chosen_ID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public AffectationProjetManager getApManager() {
		return ApManager;
	}
	public void setApManager(AffectationProjetManager apManager) {
		ApManager = apManager;
	}
	public List<MyFile> getFileList() {
		return fileList;
	}
	public void setFileList(List<MyFile> fileList) {
		this.fileList = fileList;
	}
	public MyFile getMyFile() {
		return myFile;
	}
	public void setMyFile(MyFile myFile) {
		this.myFile = myFile;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public List<User> getUsersProjet() {
		return usersProjet;
	}
	public void setUsersProjet(List<User> usersProjet) {
		this.usersProjet = usersProjet;
	}
	public List<User> getUsers_chosen() {
		return users_chosen;
	}
	public void setUsers_chosen(List<User> users_chosen) {
		this.users_chosen = users_chosen;
	}
	public List<Long> getUsers_chosen_ID() {
		return users_chosen_ID;
	}
	public void setUsers_chosen_ID(List<Long> users_chosen_ID) {
		this.users_chosen_ID = users_chosen_ID;
	}
	public String getMyFichierContentType() {
		return myFichierContentType;
	}
	public void setMyFichierContentType(String myFichierContentType) {
		this.myFichierContentType = myFichierContentType;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
}
